<template>
  <div>
    <el-upload :before-upload="beforeupload" action>
      <el-button size="small" type="primary">选择图片</el-button>
    </el-upload>
  </div>
</template>

<script>
import { IP } from "../util/util";
export default {
  data() {
    return {
      form: {
        name: ""
      },
      param: "" //表单要提交的参数
    };
  },
  methods: {
    beforeupload(file) {
      console.log(file);
      //重新写一个表单上传的方法
      this.param = new FormData();
      this.param.append("file", file, file.name);
      this.onSubmit();
      return false;
    },
    //覆盖默认的上传行为
    onSubmit() {
      //表单提交的事件
      var names = this.form.name;
      this.param.append("message", names);
      this.$ajax({
        headers: {
          "Content-Type": "multipart/form-data"
        },
        url: "http://" + IP + "/tp5/public/index.php/index/uploads/upload",
        method: "post",
        data: this.param
      }).then(res => {
        console.log(res);
        //   if (res) {
        //   }
      });
    }
  }
};
</script>

<style>
</style>
